package com.nannybear;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.nannybear.Communication.Connection;
import com.nannybear.Controller.ViewPuppeteer;
import com.nannybear.Model.AverageHumidity;
import com.nannybear.Model.AverageHumidityChange;
import com.nannybear.Model.AverageTemperature;
import com.nannybear.Model.AverageTemperatureChange;
import com.nannybear.Model.BabyStateChange;
import com.nannybear.Model.Brightness;
import com.nannybear.Model.BrightnessChange;
import com.nannybear.Model.Humidity;
import com.nannybear.Model.HumidityChange;
import com.nannybear.Model.HumidityThreshold;
import com.nannybear.Model.HumidityThresholdChange;
import com.nannybear.Model.LightThreshold;
import com.nannybear.Model.MultiCoLightDarkDimChanged;
import com.nannybear.Model.MultiCoLightDimBrightChanged;
import com.nannybear.Model.MultiCoResetHumChanged;
import com.nannybear.Model.MultiCoResetTempChanged;
import com.nannybear.Model.MultiCoSoundChanged;
import com.nannybear.Model.SoundThreshold;
import com.nannybear.Model.Temperature;
import com.nannybear.Model.TemperatureChange;
import com.nannybear.Model.TemperatureThreshold;
import com.nannybear.Model.TemperatureThresholdChange;
import com.nannybear.Model.Timeline;
import com.nannybear.Model.Yoda;
import com.nannybear.View.BabyStateScreen;
import com.nannybear.View.CalendarScreen;
import com.nannybear.View.HealthScreen;
import com.nannybear.View.HelpScreen;
import com.nannybear.View.HomeScreen;
import com.nannybear.View.HumidityScreen;
import com.nannybear.View.RemoteScreen;
import com.nannybear.View.Screen;
import com.nannybear.View.SettingsScreen;
import com.nannybear.View.TempScreen;

/**
 * \brief Main activity
 * \author Audrey LOISEAU
 * \date 15-03-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class MainActivity extends Activity {
    /********** DECLARATIONS **********/
    // View classes
    private Screen currentScreen;
    private HomeScreen homeScreen;
    private TempScreen tempScreen;
    private RemoteScreen remoteScreen;
    private HumidityScreen humidityScreen;
    private BabyStateScreen babyStateScreen;
    private SettingsScreen settingsScreen;
    private HelpScreen helpScreen;
    private HealthScreen healthScreen;
    private CalendarScreen calendarScreen;

    // Communication classes
    private Connection connection;

    // Model classes
    private Yoda yoda;
    private ViewPuppeteer viewPuppeteer;
    private SoundThreshold soundThreshold;
    private LightThreshold lightThreshold;
    private HumidityChange humidityChange;
    private TemperatureChange temperatureChange;
    private BrightnessChange brightnessChange;
    private BabyStateChange babyStateChange;
    private HumidityThresholdChange humidityThresholdChange;
    private TemperatureThresholdChange temperatureThresholdChange;
    private MultiCoLightDarkDimChanged multiCoLightDarkDimChanged;
    private MultiCoLightDimBrightChanged multiCoLightDimBrightChanged;
    private MultiCoResetHumChanged multiCoResetHumChanged;
    private MultiCoResetTempChanged multiCoResetTempChanged;
    private MultiCoSoundChanged multiCoSoundChanged;
    private Humidity humidity;
    private Temperature temperature;
    private Brightness brightness;
    private HumidityThreshold humidityThreshold;
    private TemperatureThreshold temperatureThreshold;
    private AverageTemperatureChange averageTemperatureChange;
    private AverageHumidityChange averageHumidityChange;
    private AverageTemperature averageTemperature;
    private AverageHumidity averageHumidity;
    private Timeline timeline;

    // Variables
    static private final String IP_RED_BRICK = "192.168.42.1";
    static private final int PORT_RED_BRICK = 12345;
    private Context context;

    /*********** METHODS ***********/
    /**
     * \brief  Load the activity and create the classes
     */
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        this.context = getApplicationContext();

        this.setContentView(R.layout.home_screen);

        //Model creation
        this.yoda = new Yoda(this);
        this.lightThreshold = new LightThreshold();
        this.soundThreshold = new SoundThreshold();
        this.humidityChange = new HumidityChange(this);
        this.temperatureChange = new TemperatureChange(this);
        this.brightnessChange = new BrightnessChange(this);
        this.babyStateChange = new BabyStateChange(this);
        this.humidityThresholdChange = new HumidityThresholdChange(this);
        this.temperatureThresholdChange = new TemperatureThresholdChange(this);
        this.averageTemperatureChange = new AverageTemperatureChange(this);
        this.averageHumidityChange = new AverageHumidityChange(this);
        this.multiCoLightDarkDimChanged = new MultiCoLightDarkDimChanged(this);
        this.multiCoLightDimBrightChanged = new MultiCoLightDimBrightChanged(this);
        this.multiCoResetHumChanged = new MultiCoResetHumChanged(this);
        this.multiCoResetTempChanged = new MultiCoResetTempChanged(this);
        this.multiCoSoundChanged = new MultiCoSoundChanged(this);
        this.humidity = new Humidity();
        this.temperature = new Temperature();
        this.brightness = new Brightness();
        this.humidityThreshold = new HumidityThreshold();
        this.temperatureThreshold = new TemperatureThreshold();
        this.averageTemperature = new AverageTemperature();
        this.averageHumidity = new AverageHumidity();
        this.timeline = new Timeline();

        //View creation
        this.homeScreen = new HomeScreen();
        this.tempScreen = new TempScreen();
        this.remoteScreen = new RemoteScreen();
        this.humidityScreen = new HumidityScreen();
        this.babyStateScreen = new BabyStateScreen();
        this.settingsScreen = new SettingsScreen();
        this.helpScreen = new HelpScreen();
        this.healthScreen = new HealthScreen();
        this.calendarScreen = new CalendarScreen();

        this.currentScreen = this.homeScreen;

        //Controller creation
        viewPuppeteer = new ViewPuppeteer(this);


        //Retrieves data saved
        yoda.retrieveEventFromJSON("events");

        // Connection creation
        this.connection = new Connection(this, IP_RED_BRICK, PORT_RED_BRICK);
    }

    /**
     * \brief Save the calendar events when the application is stopped
     */
    public void onStop(){
        yoda.saveEventsToJSON("events", viewPuppeteer.getCalendarController().getEventsCalendar().getMapEvent());
        super.onStop();
    }

    /**
     * \brief Send the threshold saved in shared preferences
     */
    public void sendThreshold(){
        String lightThresholdValue = String.valueOf((int)yoda.retrievesSharedPreferencesLong("lightValue_dark_dim", 55))+"/"+String.valueOf((int)yoda.retrievesSharedPreferencesLong("lightValue_dim_bright", 80));
        this.lightThreshold.setLightThresholds(lightThresholdValue);
        this.soundThreshold.setValueSound((int)yoda.retrievesSharedPreferencesLong("soundValue", 148));
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Get method for the current screen
     */
    public Screen getCurrentScreen() {
        return currentScreen;
    }

    /**
     * \brief Get method for the Home screen
     */
    public HomeScreen getHomeScreen() {
        return homeScreen;
    }

    /**
     * \brief Get method for the Temperature screen
     */
    public TempScreen getTempScreen() {
        return tempScreen;
    }

    /**
     * \brief Get method for the Remote screen
     */
    public RemoteScreen getRemoteScreen(){
        return remoteScreen;
    }

    /**
     * \brief Get method for the Humidity screen
     */
    public HumidityScreen getHumidityScreen() {
        return humidityScreen;
    }

    /**
     * \brief Get method for the Baby state screen
     */
    public BabyStateScreen getBabyStateScreen() {
        return babyStateScreen;
    }

    /**
     * \brief Get method for the Settings screen
     */
    public SettingsScreen getSettingsScreen() {
        return settingsScreen;
    }

    /**
     * \brief Get method for the Help screen
     */
    public HelpScreen getHelpScreen() {
        return helpScreen;
    }

    /**
     * \brief Get method for the Health screen
     */
    public HealthScreen getHealthScreen() {
        return healthScreen;
    }

    /**
     * \brief Get method for the Calendar screen
     */
    public CalendarScreen getCalendarScreen() {
        return calendarScreen;
    }

    /**
     * \brief Get method for the LightThreshold class
     */
    public LightThreshold getLightThreshold() {
        return lightThreshold;
    }

    /**
     * \brief Get method for the SoundThreshold class
     */
    public SoundThreshold getSoundThreshold() {
        return soundThreshold;
    }

    /**
     * \brief Get method for the TemperatureChange class
     */
    public TemperatureChange getTemperatureChange() {
        return temperatureChange;
    }

    /**
     * \brief Get method for the Temperature class
     */
    public Temperature getTemperature() {
        return temperature;
    }

    /**
     * \brief Get method for the HumidityChange class
     */
    public HumidityChange getHumidityChange() {
        return humidityChange;
    }

    /**
     * \brief Get method for the BrightnessChange class
     */
    public BrightnessChange getBrightnessChange() {
        return brightnessChange;
    }

    /**
     * \brief Get method for the BabyStateChange class
     */
    public BabyStateChange getBabyStateChange() {
        return babyStateChange;
    }

    /**
     * \brief Get method for the HumidityThresholdChange class
     */
    public HumidityThresholdChange getHumidityThresholdChange() {
        return humidityThresholdChange;
    }

    /**
     * \brief Get method for the TemperatureThresholdChange class
     */
    public TemperatureThresholdChange getTemperatureThresholdChange() {
        return temperatureThresholdChange;
    }

    /**
     * \brief Get method for the Humidity class
     */
    public Humidity getHumidity() {
        return humidity;
    }

    /**
     * \brief Get method for the Brightness class
     */
    public Brightness getBrightness() {
        return brightness;
    }

    /**
     * \brief Get method for the HumidityThreshold class
     */
    public HumidityThreshold getHumidityThreshold() {
        return humidityThreshold;
    }

    /**
     * \brief Get method for the TemperatureThreshold class
     */
    public TemperatureThreshold getTemperatureThreshold() {
        return temperatureThreshold;
    }

    /**
     * \brief Get method for the ViewPuppeteer class
     */
    public ViewPuppeteer getViewPuppeteer() {
        return viewPuppeteer;
    }

    /**
     * \brief Get method for the AverageTemperature class
     */
    public AverageTemperature getAverageTemperature() {
        return averageTemperature;
    }

    /**
     * \brief Get method for the AverageHumidity class
     */
    public AverageHumidity getAverageHumidity() {
        return averageHumidity;
    }

    /**
     * \brief Get method for the AverageTemperatureChange class
     */
    public AverageTemperatureChange getAverageTemperatureChange() {
        return averageTemperatureChange;
    }

    /**
     * \brief Get method for the AverageHumidityChange class
     */
    public AverageHumidityChange getAverageHumidityChange() {
        return averageHumidityChange;
    }

    /**
     * \brief Get method for the Connection class
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * \brief Get method for the Timeline class
     */
    public Timeline getTimeline() {
        return timeline;
    }

    /**
     * \brief Get method for the context
     */
    public Context getContext() {
        return context;
    }

    /**
     * \brief Get method for the Yoda class
     */
    public Yoda getYoda() {
        return yoda;
    }

    /**
     * \brief Get method for the MultiCoLightDarkDimChanged class
     */
    public MultiCoLightDarkDimChanged getMultiCoLightDarkDimChanged() {
        return multiCoLightDarkDimChanged;
    }

    /**
     * \brief Get method for the MultiCoLightDimBrightChanged class
     */
    public MultiCoLightDimBrightChanged getMultiCoLightDimBrightChanged() {
        return multiCoLightDimBrightChanged;
    }

    /**
     * \brief Get method for the MultiCoResetHumChanged class
     */
    public MultiCoResetHumChanged getMultiCoResetHumChanged() {
        return multiCoResetHumChanged;
    }

    /**
     * \brief Get method for the MultiCoResetTempChanged class
     */
    public MultiCoResetTempChanged getMultiCoResetTempChanged() {
        return multiCoResetTempChanged;
    }

    /**
     * \brief Get method for the MultiCoSoundChanged class
     */
    public MultiCoSoundChanged getMultiCoSoundChanged() {
        return multiCoSoundChanged;
    }

    /**
     * \brief Set method for the Connection class
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * \brief Set method for the current screen
     */
    public void setCurrentScreen(Screen currentScr) {
        this.currentScreen = currentScr;
    }
}